﻿using System;
using System.Collections.Generic;

using RexToy.L10N;
using RexToy.UI.WinForm.Controls;
using RexToy.UI.WinForm.Pmlx;

using MGAS.Model.Entity;

namespace MGAS.UI.Controls
{
    public class SensorDataList : UserControl//, ITemplateItem
    {
        [PmlxBoundControl("lb")]
        protected System.Windows.Forms.ListBox _lb;

        class Item : IEquatable<Item>
        {
            public int Value { get; set; }
            public PressureUnit Unit { get; set; }
            public PressureStatus Status { get; set; }
            public DateTime CollectTime { get; set; }

            public bool Equals(Item other)
            {
                return this.Value == other.Value && this.Unit == other.Unit && this.Status == other.Status;
            }

            public override string ToString()
            {
                //TODO:L10N, time format
                return string.Format("{0}\t{1}\t{2} [{3}]", this.CollectTime, this.Status, this.Value, this.Unit);
            }
        }

        public SensorDataList()
        {
            this.Lines = 5;
        }

        public int Lines { get; set; }

        public void Append(int val, PressureStatus status, PressureUnit unit)
        {
            Item item = new Item() { Value = val, Unit = unit, Status = status, CollectTime = DateTime.Now };
            if (_lb.Items.Count == 0)
            {
                _lb.Items.Add(item);
                return;
            }

            Item lastItem = (Item)_lb.Items[_lb.Items.Count - 1];
            if (item.Equals(lastItem))
                return;

            _lb.Items.Add(item);
            if (_lb.Items.Count > Lines)
            {
                _lb.Items.RemoveAt(0);
            }
        }
    }
}
